#include "include/VectorTest.h"
#include "include/VectorObject.h"
#include "include/App.h"
#include "include/Scripting.h"

//
// private
//
using namespace Ei;
using namespace Ogre;

int VectorObject::msInstanceCount = 0;

VectorObject::VectorObject()
{
	VectorObject(Vector3(0,0,0));	
}

VectorObject::VectorObject(Vector3 pos)
{
	
	String name = "_Vector_" + StringConverter::toString(msInstanceCount++);
	
  	 /// magnetometer
        rootnode = App::sceneMgr()->getRootSceneNode()->createChildSceneNode(String("root") + name);
        rootnode->setPosition(pos);
        	            
        thickness = App::scripting()->getTableFloat("game","thickness");
        magnitude = App::scripting()->getTableFloat("game","magnitude");
		
	/// Xas
	Entity * mEntity = App::sceneMgr()->createEntity(String("X") + name, "box.mesh");
        Xnode = rootnode->createChildSceneNode();
        mEntity->setMaterialName("blue");
        Xnode->setScale(Vector3(1,thickness ,thickness ));
        Xnode->attachObject(mEntity);   

        /// Yas
        mEntity = App::sceneMgr()->createEntity(String("Y") + name, "box.mesh");
        Ynode = rootnode->createChildSceneNode();
        mEntity->setMaterialName("red");
        Ynode->setScale(Vector3(thickness ,thickness ,1));
        Ynode->attachObject(mEntity);
		
	  /// Zas
        mEntity = App::sceneMgr()->createEntity(String("Z") + name, "box.mesh");
        Znode = rootnode->createChildSceneNode();
        mEntity->setMaterialName("green");
        Znode->setScale(Vector3(thickness ,1,thickness ));
        Znode->attachObject(mEntity);	
}
VectorObject::~VectorObject()
{
	// just remove all children and objects from the scene
	// our great ogre scene manager will take care of freeing memory
	// ...... i hope :> 
	rootnode->detachAllObjects();
	rootnode->removeAndDestroyAllChildren();
}

//
// public
//

void VectorObject::setScale(Vector3 scale)
{	
	scale = scale * magnitude;
	Xnode->setScale(Vector3(scale.x  ,thickness ,thickness ));
	Ynode->setScale(Vector3(thickness,scale.y,thickness   ));
	Znode->setScale(Vector3(thickness,thickness , scale.z   ));	
}


void VectorObject::setPosition(Vector3 pos)
{
	rootnode->setPosition(pos);	
}

void VectorObject::setOrientation(Quaternion quat)
{
	rootnode->setOrientation(quat);	
}

void VectorObject::setColour(int Scheme)
{
	Entity * tempEntity;	
	switch(Scheme)
	{
		case 0:
		{
		 tempEntity = (Entity*) Xnode->getAttachedObject(0);
		 tempEntity->setMaterialName("red");
		 tempEntity = (Entity*) Ynode->getAttachedObject(0);
		 tempEntity->setMaterialName("green");
		 tempEntity = (Entity*) Znode->getAttachedObject(0);
		 tempEntity->setMaterialName("blue");		 
	  	 break;	
		}
		case 1:
		{
		 tempEntity = (Entity*) Xnode->getAttachedObject(0);
		 tempEntity->setMaterialName("yellow");
		 tempEntity = (Entity*) Ynode->getAttachedObject(0);
		 tempEntity->setMaterialName("turq");
		 tempEntity = (Entity*) Znode->getAttachedObject(0);
		 tempEntity->setMaterialName("lila");		
		 break;	
		}
		case 2:
		{
		 tempEntity = (Entity*) Xnode->getAttachedObject(0);
		 tempEntity->setMaterialName("orange");
		 tempEntity = (Entity*) Ynode->getAttachedObject(0);
		 tempEntity->setMaterialName("navy");
		 tempEntity = (Entity*) Znode->getAttachedObject(0);
		 tempEntity->setMaterialName("purple");		
		 break;	
		}
		default:
		{
		 tempEntity = (Entity*) Xnode->getAttachedObject(0);
		 tempEntity->setMaterialName("red");
		 tempEntity = (Entity*) Ynode->getAttachedObject(0);
		 tempEntity->setMaterialName("green");
		 tempEntity = (Entity*) Znode->getAttachedObject(0);
		 tempEntity->setMaterialName("blue");	
		 break;	
		}			
	}	
}

void VectorObject::show(bool showObject)
{
 
 rootnode->setVisible(showObject);		
}



